<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Admin\AdminService;
use Illuminate\Http\Request;

class BaseController extends Controller
{
    public function __construct()
    {
    }

    //跨域处理
    public function cors()
    {
        header("Access-Control-Allow-Origin: *");
    }

    /** 
     * 参数中获取aif
    */
    public function getAid(Request $request)
    {
        //数据验证
        $this->validate($request, [
            'aid' => 'required',
        ], [
            'aid.required' => '缺少必要参数',
        ]);
        $pas = $request->all();
        //没有登录或已过期
        $rst = $this->checkLogin($pas['aid']);
        if ($rst['code'] != 0) {
            return array('code' => -2, 'msg' => $rst['msg']);
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $rst['data']);
    }

    /**
     * 验证登录
     * return array
     */
    public function checkLogin($adminid)
    {
        $adminObj = new AdminService();
        $res = $adminObj->getLog($adminid);
        if (empty($res)) {
            return array('code' => -2, 'msg' => '没有登录');
        }
        $currTime = date('Y-m-d') . ' 00:00:01';
        if ($res['addtime'] < $currTime) {
            return array('code' => -2, 'msg' => '登录已过期');
        }
        return array('code' => 0, 'msg' => 'success', 'data' => $res);
    }
}